package com.hmkj.admin.controller.user;


import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.core.po.account.Account;
import com.hmkj.core.po.order.Order;
import com.hmkj.core.po.system.Admin;
import com.hmkj.core.po.user.User;
import com.hmkj.core.constant.Enum;
import com.hmkj.core.service.account.AccountService;
import com.hmkj.core.service.order.OrderService;
import com.hmkj.core.service.user.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class UserController extends BaseController {

    @Resource
    private UserService userService;
    @Resource
    private AccountService accountService;
    @Resource
    private OrderService orderService;

    /**
     * 用户列表
     *
     * @return
     */
    @RequestMapping("user/list")
    public String list() {
        return ok("user/user/list");
    }

    /**
     * 代理商列表
     */
    @RequestMapping("/user/agentList")
    public String agentList() {
        return ok("user/agent/list");
    }

    /**
     * 我的代理商
     */
    @RequestMapping("/user/myAgentList")
    public String myAgentList(HttpServletRequest request) {
        Admin admin = AdminSessionHelper.getCurrAdmin();
        User user = new User();
        if(admin.getRoleId()== Enum.PLATFORM_ROLE_ID||admin.getRoleId()== Enum.SELLER_ROLE_ID){
            user.setPoundageScale(new BigDecimal(100));
            user.setProfitScale(new BigDecimal(100));
            user.setDeferScale(new BigDecimal(100));
        }else{
            user = userService.selectByPrimaryKey(admin.getUserId());
        }
        request.setAttribute("user", user);
        return getAdminTemplate("user/agent/myList");
    }

    /**
     * 结算报表
     */
    @RequestMapping("/user/profitReport")
    public String profitReport(){
        return getAdminTemplate("user/agent/profitReport");
    }

    /**
     * 客户详情
     */
    @RequestMapping(value = "/user/look", method = RequestMethod.GET)
    public String lookUser(Integer id, ModelMap modelMap) throws Exception {
        User user = userService.selectByPrimaryKey(id);
        Account account = accountService.selectOne(new Account(t -> {
            t.setUserId(id);
        }));
        //方案总数
        long orderCount = orderService.selectCount(new Order(t->{
            t.setUserId(id);
        }));
        modelMap.put("user", user);
        modelMap.put("account", account);
        modelMap.put("totalOrder", orderCount);
        return ok("/user/user/look");
    }

    /**
     * 我的下线
     */
    @RequestMapping(value = "user/myInvestor", method = RequestMethod.GET)
    public String myInvestor(User use, ModelMap modelMap) throws Exception {
        User user = userService.selectByPrimaryKey(use.getId());
        modelMap.addAttribute("user", user);
        Account account = accountService.selectOne(new Account(v->{
             v.setUserId(user.getId());
        }));
        modelMap.addAttribute("account", account);
        //方案总数
        long orderCount = orderService.countOrders(user.getId());
        modelMap.addAttribute("totalOrder", orderCount);
        return getAdminTemplate("/user/user/myInvestor");
    }

    /**
     * 我的银行
     */
    @RequestMapping("user/myBank")
    public String myBank(User use, ModelMap modelMap) throws Exception {
        User user = userService.selectByPrimaryKey(use.getId());
        modelMap.addAttribute("user", user);
        Account account = accountService.selectOne(new Account(a->{
             a.setUserId(user.getId());
        }));
        modelMap.addAttribute("account", account);
        //方案总数
        long orderCount = orderService.countOrders(user.getId());
        modelMap.addAttribute("totalOrder", orderCount);
        return getAdminTemplate("/user/user/myBank");
    }

    /**
     * 资金明细
     */
    @RequestMapping(value = "user/moneyDetails", method = RequestMethod.GET)
    public String moneyDetail(User use, ModelMap modelMap) throws Exception {
        User user = userService.selectByPrimaryKey(use.getId());
        modelMap.addAttribute("user", user);
        Account account = accountService.selectOne(new Account(a->{
             a.setUserId(user.getId());
        }));
        modelMap.addAttribute("account", account);
        //方案总数
        long orderCount = orderService.countOrders(user.getId());
        modelMap.addAttribute("totalOrder", orderCount);
        return ok("/user/user/moneyDetail");
    }
}
